import request from '@/utils/request'
import {getParamString,GeoServer} from '@/utils/geotools'

// 根据经纬度，获取图层对象。
export function getBuildNames(lon,lat,typeName = GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding) {
  return request({
    url: GeoServer.ntGeoWmsUrl+'?service=WFS&version=1.0.0%0A&request=GetFeature&typeName='+typeName+
        '&maxFeatures=50%0A&outputFormat=application%2Fjson%20&filter=%3CFilter%20xmlns=%22http://www.opengis.net/ogc%22%20xmlns:gml=%22http://www.opengis.net/gml%22%3E%20%20%0A%20%20%20%20%3CIntersects%3E%0A%09%3CPropertyName%3Ethe_geom%3C/PropertyName%3E%0A%09%3Cgml:Point%3E%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%3Cgml:coordinates%3E'+lon+','+lat+'%3C/gml:coordinates%3E%20%0A%20%20%20%20%3C/gml:Point%3E%20%09%0A%20%20%20%20%3C/Intersects%3E%0A%3C/Filter%3E',

    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}
// 根据经纬度，获取图层对象。
export function getItemsByLonLat(workspaceName=GeoServer.ntGeoWorkspace,layerName = GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding,lon,lat) {

  let baseUrl = 'geoserver/nantong/ows?service=WFS&version=1.0.0\n' +
      '&request=GetFeature&typeName=nantong:nt_building_2020&maxFeatures=50\n' +
      '&outputFormat=application%2Fjson\n' +
      '&filter=<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">  \n' +
      '    <Intersects>\n' +
      '\t<PropertyName>the_geom</PropertyName>\n' +
      '\t<gml:Point>   \n' +
      '          <gml:coordinates>120.93314,31.99886</gml:coordinates> \n' +
      '    </gml:Point> \t\n' +
      '    </Intersects>\n' +
      '</Filter>'

  baseUrl= '/geoserver/'+workspaceName+'/ows?service=WFS&version=1.0.0%0A&request=GetFeature&typeName='+layerName+
      '&maxFeatures=50%0A&outputFormat=application%2Fjson%20&filter=%3CFilter%20xmlns=%22http://www.opengis.net/ogc%22%20xmlns:gml=%22http://www.opengis.net/gml%22%3E%20%20%0A%20%20%20%20%3CIntersects%3E%0A%09%3CPropertyName%3Ethe_geom%3C/PropertyName%3E%0A%09%3Cgml:Point%3E%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%3C' +
      'gml:coordinates%3E'+lon+','+lat+'%3C/gml:coordinates%3E%20%0A%20%20%20%20%3C/gml:Point%3E%20%09%0A%20%20%20%20%3C/Intersects%3E%0A%3C/Filter%3E';


  return request({
    // url: '/geoserver/'+workspaceName+'/ows?service=WFS&version=1.0.0%0A&request=GetFeature&typeName='+layerName+
    //     '&maxFeatures=50%0A&outputFormat=application%2Fjson%20&filter=%3CFilter%20xmlns=%22http://www.opengis.net/ogc%22%20xmlns:gml=%22http://www.opengis.net/gml%22%3E%20%20%0A%20%20%20%20%3CIntersects%3E%0A%09%3CPropertyName%3Ethe_geom%3C/PropertyName%3E%0A%09%3Cgml:Point%3E%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%3C' +
    //     'gml:coordinates%3E'+lon+','+lat+'%3C/gml:coordinates%3E%20%0A%20%20%20%20%3C/gml:Point%3E%20%09%0A%20%20%20%20%3C/Intersects%3E%0A%3C/Filter%3E',
    url:baseUrl,

    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}
// 根据范围，获取图层对象。
export function getItemsByArea(workspaceName=GeoServer.ntGeoWorkspace,layerName =GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding,points=[[],[],[],[]]) {

  let urlStr= GeoServer.ntGeoWmsUrl+'?service=WFS&version=1.0.0&request=GetFeature&typeName='+layerName+'&outputFormat=application/json&filter=' +
      '<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><And><Intersects><PropertyName>the_geom</PropertyName><gml:Polygon>' +
      '<gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>120.939132,32.009905 120.941650,32.009905 120.941650,32.008028 120.939132,32.008028 120.939132,32.009905 </gml:coordinates></gml:LinearRing>' +
      '</gml:outerBoundaryIs></gml:Polygon></Intersects></And></Filter>'


  return request({
    url:urlStr,
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}
// 根据属性值，获取图层对象。
export function getItemsByColumnName(workspaceName=GeoServer.ntGeoWorkspace,layerName =GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding,columnName = 'name_1',columnValue='3号楼') {

  return request({
    url:'/geoserver/'+workspaceName+'/ows?service=WFS&version=1.0.0&request=GetFeature&typeName='+layerName +
        '&maxFeatures=50%2520&outputFormat=application/json&CQL_FILTER='+columnName+'+like+%27%25'+columnValue+'%25%27',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}
// 根FID，获取图层对象。
export function getItemsByFeatureID(workspaceName=GeoServer.ntGeoWorkspace,layerName =GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding,featureID='nt_poi_gy_2020.3') {

  return request({
    url:'/geoserver/'+workspaceName+'/ows?service=WFS&version=1.0.0&request=GetFeature&typeName='+layerName +
        '&maxFeatures=50%2520&outputFormat=application/json&featureID='+featureID,
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}
// 根据属性值数组，获取图层对象。
export function getItemsByColumnNames(workspaceName=GeoServer.ntGeoWorkspace,layerName = GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding,columnNames = ['name_1'],columnValues=['3号楼']) {

  let strUrl = '/geoserver/'+workspaceName+'/ows?service=WFS&version=1.0.0&request=GetFeature&typeName='+layerName +
      '&outputFormat=application/json&CQL_FILTER=';
  if(columnNames.length&&columnValues.length&& columnNames.length === columnValues.length && columnNames.length>0){
    for(let i=0;i<columnNames.length;i++){
      if(i>0){
        strUrl = strUrl+' and  '
      }

      strUrl = strUrl+ ' ' + columnNames[i]+'+like+%27%25'+columnValues[i]+'%25%27 '

    }

  }
  console.log(strUrl)
  return request({
    url:strUrl,
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}
// 根据属性值数组，获取图层对象。
export function getItems(workspaceName=GeoServer.ntGeoWorkspace,layerName = GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding) {

  let strUrl = '/geoserver/'+workspaceName+'/ows?service=WFS&version=1.0.0&request=GetFeature&typeName='+layerName +
      '&maxFeatures=50%2520&outputFormat=application/json';//&CQL_FILTER=EXT1+like+%27%252%25%27
  console.log(strUrl);
  return request({
    url:strUrl,
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}

//根据图层获取所有数据
// 根据属性值数组，获取图层对象。
export function getAllItemsByColumnNames(workspaceName=GeoServer.ntGeoWorkspace,layerName = GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding) {

  let strUrl = '/geoserver/'+workspaceName+'/ows?service=WFS&version=1.0.0&request=GetFeature&typeName='+layerName +
      '&maxFeatures=50%2520&outputFormat=application/json';

  console.log(strUrl);

  return request({
    url:strUrl,
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}

// 根据属性值数组，获取图层对象。
export function deleteItemByFid(id,workspaceName=GeoServer.ntGeoWorkspace,layerName = GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding) {
  let xml = '<Transaction xmlns="http://www.opengis.net/wfs" service="WFS" version="1.0.0"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.opengis.net/wfs   http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">';
  xml += '<Delete typeName="'+layerName+'">';
  xml += '<Filter xmlns="http://www.opengis.net/ogc">';
  xml += '<FeatureId fid="' + id + '"/>';
  xml += '</Filter>';
  xml += '</Delete>';
  xml += '</Transaction>';
  let strUrl = '/geoserver/'+workspaceName+'/wfs'
  return request({
    url:strUrl,
    headers: {
      isToken: false
    },
    method: 'post',
    data: xml,
    contentType: 'text/xml',
    timeout: 20000
  })
}
// 根据属性值数组，获取图层对象。
export function updateItemByColumnValues(id,columns,values,workspaceName=GeoServer.ntGeoWorkspace,layerName = GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding) {
  let xml = '<wfs:Transaction service="WFS" version="1.0.0"    xmlns:opengis="http://webgis.com"     xmlns:wfs="http://www.opengis.net/wfs"    xmlns:ogc="http://www.opengis.net/ogc"    xmlns:gml="http://www.opengis.net/gml"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.opengis.net/wfs   http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">';
  xml += '<wfs:Update typeName="'+layerName+'">';
  for(let i=0;i<columns.length;i++){

    xml += '<wfs:Property>';
    xml += '<wfs:Name>'+columns[i]+'</wfs:Name>';
    xml += '<wfs:Value>' +values[i] + '</wfs:Value>';
    xml += '</wfs:Property>';
  }

  xml += '<ogc:Filter>';
  xml += '<ogc:FeatureId fid="' + id + '"/>';
  xml += '</ogc:Filter>';
  xml += '</wfs:Update>';
  xml += ' </wfs:Transaction>';
  let strUrl = '/geoserver/'+workspaceName+'/wfs'
  return request({
    url:strUrl,
    headers: {
      isToken: false
    },
    method: 'post',
    data: xml,
    contentType: 'text/xml',
    timeout: 20000
  })
}
// 根据属性值数组，获取图层对象。
export function updateItems() {
  let xml = '<wfs:Transaction service="WFS" version="1.0.0"    xmlns:opengis="http://webgis.com"     xmlns:wfs="http://www.opengis.net/wfs"    xmlns:ogc="http://www.opengis.net/ogc"    xmlns:gml="http://www.opengis.net/gml"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.opengis.net/wfs   http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">';
  xml += '<wfs:Update typeName="'+GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding+'">';
  // xml += '<wfs:Property>';
  //     xml += '<wfs:Name>the_geom</wfs:Name>';
  //     xml += '<wfs:Value>';
  //         xml += '<gml:LineString>';
  //             xml += '<gml:coordinates decimal="." cs="," ts=" ">' + polygon + '</gml:coordinates>';
  //         xml += '</gml:LineString>';
  //     xml += '</wfs:Value>';
  // xml += '</wfs:Property>';
  xml += '<wfs:Property>';
  xml += '<wfs:Name>name_1</wfs:Name>';
  xml += '<wfs:Value>' + 'fieldValue1' + '</wfs:Value>';
  xml += '</wfs:Property>';
  xml += '<wfs:Property>';
  xml += '<wfs:Name>name</wfs:Name>';
  xml += '<wfs:Value>' + 'fieldValue2' + '</wfs:Value>';
  xml += '</wfs:Property>';
  xml += '<ogc:Filter>';
  xml += '<ogc:FeatureId fid="' + 'building.14' + '"/>';
  xml += '</ogc:Filter>';
  xml += '</wfs:Update>';
  xml += ' </wfs:Transaction>';
  let strUrl = '/geoserver/nantong/wfs'
  return request({
    url:strUrl,
    headers: {
      isToken: false
    },
    method: 'post',
    data: xml,
    contentType: 'text/xml',
    timeout: 20000
  })
}

export function queryByPoint(point, typeName=GeoServer.layersName.layersNameBuilding){
  let filter =
      '<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">';
  filter += '<Intersects>';
  filter += '<PropertyName>the_geom</PropertyName>';
  filter += '<gml:Point>';
  filter += '<gml:coordinates>' + point + '</gml:coordinates>';
  filter += '</gml:Point>';
  filter += '</Intersects>';
  filter += '</Filter>';
  let urlString = '/geoserver/nantong/ows';
  let param = {
    service: 'WFS',
    version: '1.0.0',
    request: 'GetFeature',
    typeName: typeName,
    outputFormat: 'application/json',
    filter: filter
  };
  let geojsonUrl = urlString + getParamString(param, urlString);


  return request({
    url:geojsonUrl,
    headers: {
      isToken: false
    },
    method: 'get',
    contentType: 'text/xml',
    dataType: 'json',
    timeout: 20000
  })

}
// 根据属性值数组，获取图层组对象。
export function getItemsTogroupLayers(workspaceName=GeoServer.ntGeoWorkspace,layerName = GeoServer.ntGeoWorkspace+':'+GeoServer.layersName.layersNameBuilding,columnNames = ['name_1'],columnValues=['3号楼']) {

  let strUrl = '/geoserver/'+workspaceName+'/ows?service=WFS&version=1.0.0&request=GetFeature&layergroupName='+layerName +
      '&maxFeatures=50%2520&outputFormat=application/json&CQL_FILTER=';
  console.log(strUrl);
  if(columnNames.length&&columnValues.length&& columnNames.length === columnValues.length && columnNames.length>0){
    for(let i=0;i<columnNames.length;i++){
      if(i>0){
        strUrl = strUrl+' and  '
      }

      strUrl = strUrl+ ' ' + columnNames[i]+'+like+%27%25'+columnValues[i]+'%25%27 '

    }

  }

  return request({
    url:strUrl,
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}
